<?php
/*
 * @Author: 一品网络技术有限公司
 * @Date: 2022-07-24 07:53:52
 * @LastEditTime: 2022-08-09 13:09:26
 * @FilePath: \web\app\common\model\Files.php
 * @Description:
 * 联系QQ:58055648
 * Copyright (c) 2022 by 东海县一品网络技术有限公司, All Rights Reserved.
 */

declare(strict_types=1);

namespace app\admin\model;

use think\Model;
use think\facade\Cache;
use think\facade\Request;
//本模型为系统生成,具体功能需要您来配置
class files extends Model
{
    public static function onAfterRead($data)
    {
        
        if($data){
            $siteurl = Request::root(true);
            $data->filepath = $siteurl . $data->filepath;
        }

        return $data;
    }
    public static function 	onBeforeInsert($files)
    {
        $files->set('ftype',getFileType(public_path().$files->getData()['filepath']));
        return $files;
    }
    //模型事件-删除后
    public static function onAfterDelete($files)
    {
        //删除数据后删除附件
        if(!empty($files['filepath'])){
            $url = Request::root(true);
            $img_url = public_path().str_replace($url,'',$files['filepath']);
        }
        if (file_exists($img_url)) {
            $dirname = dirname ($img_url);
            unlink($img_url);
            if(count(scandir($dirname)) == 2){
                rmdir($dirname);
            }
        }
    }
    //绑定信息
    public static function bindInfo($ids, $id, $category_id = 0, $type="", $tag="")
    {
        //绑定信息前解除当前绑定的信息
        self::where(['ypcms_id' => $id, 'ypcms_type' => $type, 'tag' => $tag])->update(['ypcms_id' => '', 'ypcms_type' => '', 'tag' => '']);
        //绑定多条
        if (is_array($ids)) {
            foreach ($ids as $key => $value) {
                self::update(['id' => $value, 'category_id' => $category_id, 'ypcms_id' => $id, 'ypcms_type' => $type, 'tag' => $tag]);
            }
        } else {
            //绑定单条
            self::update(['id' => $ids, 'category_id' => $category_id, 'ypcms_id' => $id, 'ypcms_type' => $type, 'tag' => $tag]);
        }
    }
    //绑定内容中图片
    public static function bindEditor($id, $category_id = 0, $type="", $tag="", $content="")
    {
        //内容图片绑定
        self::where(['ypcms_id' => $id, 'ypcms_type' => $type, 'tag' => $tag])->update(['ypcms_id' => '', 'ypcms_type' => '', 'tag' => '']);
        preg_match_all('/<img(.*?)src=\"(.*?)\"(.*?)>/i', $content, $res);
        $site=cache::get('sitepro');

        foreach ($res[2] as $key => $value) {
            $value=str_replace('//'.$site['siteurl'],'',$value);
            self::where('filepath', $value)->update(['category_id' => $category_id, 'ypcms_id' => $id, 'ypcms_type' => $type, 'tag' => $tag]);
        }
    }
    //栏目信息
    public function category(){
        return $this->belongsTo(Category::class);
    }
    //模型信息
    public function getModAttr($value,$data){
        return Mod::where('name','=',$data['ypcms_type'])->find();
    }

    public function ypcms()
    {
        return $this->morphTo('ypcms', [
            'Goods' => 'app\common\model\Goods',
            'Article' => 'app\common\model\Article',
            'System' => 'app\common\model\System',
        ]);
    }
}
